<?php

class Core_Frontend_Module_Article_Latest extends Dcmp_Module_Base {

    protected $name = 'article_latest';

    public function install() {

    }

    public function uninstall() {

    }

    public function buildConfig($form) {
        $categories = App::getModel('article/collection_category')->load();
        $form->addComboTreeField($this->__('article.label.category'), 'category', "module[parameters][category]")
                ->bindData($categories, 0, array('valueKey' => 'id', 'textKey' => 'title', 'parentKey' => 'parent_id', 'childKey' => 'id'))
                ->setDefaultOption('all', $this->__('share.label.all'));
        $form->addSelectField($this->__('article.label.condition'), 'condition', 'module[parameters][condition]')
                ->setOption($this->__('article.label.clickcount'), 'clickcount')
                ->setOption($this->__('article.label.modified_time'), 'modified_time')
                ->setOption($this->__('article.label.hot'), 'hot')
                ->setOption($this->__('article.label.new'), 'new')
                ->setOption($this->__('article.label.top'), 'top');
        $form->addTextBoxField($this->__('share.label.item_count'), 'item_count', 'module[parameters][item_count]')
                ->addRequiredRule()
                ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.introduction_count'), 'introduction_count', 'module[parameters][introduction_count]')
                ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.introduction_size'), 'introduction_size', 'module[parameters][introduction_size]')
                ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.image_count'), 'image_count', 'module[parameters][image_count]')
                ->setValue(0)
                ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.image_width'), 'image_width', 'module[parameters][image_width]')
                ->setValue(0)
                ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.image_height'), 'image_height', 'module[parameters][image_height]')
                ->setValue(0)
                ->addDigitsRule();
        $form->createTab('module_option', $this->__('design.label.module_parameter'),
                array('field_category', 'field_condition', 'field_item_count', 'field_introduction_count',
                      'field_introduction_size', 'field_image_count', 'field_image_width', 'field_image_height'));
    }

    public function render() {
        $contents = App::getModel('article/collection_content')
                        ->resetSelect()->field('a.title')->field('introduction')->field('picture')->field('a.id')->field('a.category_id')
                        ->filterByStatus('published')
                        ->filterByStartTime(date('Y-m-d H:m:s'))
                        ->filterByEndTime(date('Y-m-d H:m:s'))
                        ->joinCategory()
                        ->sortBy($this->getValueByKey('condition'), 'desc')
                        ->limit($this->getValueByKey('item_count'),0);
        if($this->getValueByKey('category')=="all") $contents->load();
        else {
            $categoryIds = App::getModel('article/category')->load($this->getValueByKey('category'))->getChildren();
            if(count($categoryIds)){
            foreach($categoryIds as $categoryId){
                $ids[] = $categoryId->getId();
            }
            $contents->filterByCategoryIds($ids)->load();
            }
            else $contents->filterByCategoryIds($this->getValueByKey('category'))->load();
        }
        $obj = new Kd_Object($this->getParameters());
        $nameStartIndex = 0;
        if($obj->getImageCount()){
            $obj->setIsShowImage(TRUE);
            $obj->setStartImageIndex(0);
            $obj->setEndImageIndex(min(count($contents), $obj->getImageCount()));
            $nameStartIndex = max($obj->getEndImageIndex(), $nameStartIndex);
        }
        if($obj->getIntroductionCount()){
            $obj->setIsShowIntroduction(TRUE);
            $obj->setStartIntroductionIndex($nameStartIndex);
            $obj->setEndIntroductionIndex(min(count($contents), $obj->getIntroductionCount()));
            $nameStartIndex = max($obj->getEndIntroductionIndex(), $nameStartIndex);
        }
        $obj->setStartNameIndex($nameStartIndex);
        $obj->setEndNameIndex(min(count($contents), $obj->getItemCount()));
        $tmpl = $this->getTemplate()->assignMulti(array('contents' => $contents, 'parameters' => $obj));
        return $tmpl->render($this->getTemplatePath($this->getValueByKey('template_name'),$this->getValueByKey('template_script')));
    }

}

